<?php
namespace Home\Controller;
use Think\Controller;
class CommunalController extends Controller{
	const YES=1;//true
	const NO=0;//flase
	private $login_id=0;
	private $login_username='';
	private $login_department='';
	private $login_department_id='';
	private $login_permissions_rule='';
	private $login_permissions_id='';
	private $bool_ip=0;

	//初始方法 designed by:zhangsen 2017-09-20
	function __construct(){
		parent::__construct();
		$this->login_id=$_SESSION['dms_uid'];
		$this->assign('login_id',$this->login_id);
		$this->login_username=$_SESSION['dms_username'];
		$this->assign('login_username',$this->login_username);
		$this->login_department=$_SESSION['dms_department'];
		$this->assign('login_department',$this->login_department);
		$this->login_department_id=$_SESSION['dms_department_id'];
		$this->assign('login_department_id',$this->login_department_id);
		$this->login_permissions_rule=$_SESSION['dms_permissions_rule'];
		$this->assign('login_permissions_rule',$this->login_permissions_rule);
		$this->login_select_id=$_SESSION['dms_select_id'];
		$this->assign('login_select_id',$this->login_select_id);
		$this->login_sw_select_id=$_SESSION['dms_sw_select_id'];
		$this->assign('login_sw_select_id',$this->login_sw_select_id);
		$this->login_manage_id=$_SESSION['dms_manage_id'];
		$this->assign('login_manage_id',$this->login_manage_id);
		$this->login_sw_manage_id=$_SESSION['dms_sw_manage_id'];
		$this->assign('login_sw_manage_id',$this->login_sw_manage_id);
		$this->login_center_id=$_SESSION['dms_center_id'];
		$this->assign('login_center_id',$this->login_center_id);
		$this->login_permissions_id=$_SESSION['dms_permissions_id'];
		$this->assign('login_permissions_id',$this->login_permissions_id);
		$this->assign('today',date('Y-m-d'));
		$this->bool_ip=$this->bool_ip();
		$this->assign('bool_ip',$this->bool_ip);
		//操作日志
		if('login'!=ACTION_NAME&&'do_login'!=ACTION_NAME){
			$this->run_log();
		}
	}

	//初始方法 designed by:zhangsen 2016-08-11
	public function _initialize(){
		$user=$this->get_user($_SESSION['dms_uid']);
		$this->login_id=$_SESSION['dms_uid'];
		$this->login_username=$_SESSION['dms_username'];
		if(empty($this->login_id))
		{
			if('login'!=ACTION_NAME&&'do_login'!=ACTION_NAME&&'system_timing_update_dep'!=ACTION_NAME&&'system_timing_update_user'!=ACTION_NAME&&'project_funds_alert'!=ACTION_NAME)
			{
                $goto = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                cookie('goto',$goto);
				$this->error('请先登录',__APP__."/Index/login");die;
			}
		}
		$this->login_department=$this->get_department($user['department_id']);
		$this->login_department_id=$user['department_id'];
		$this->login_permissions_rule=$user['permissions_rule'];
		$this->login_select_id=explode(',',$user['select_id']);
		$this->login_sw_select_id=explode(',',$user['sw_select_id']);
		$this->login_manage_id=explode(',',$user['manage_id']);
		$this->login_sw_manage_id=explode(',',$user['sw_manage_id']);
		$this->login_permissions_id=$user['permissions_id'];
		$this->login_center_id=$user['center_id'];
		$this->assign('KindEditor_Path',KindEditor_Path);//编辑器
		$permissions_code=explode(',', $this->login_permissions_rule);
		foreach ($permissions_code as $key=> $value)
		{
			$this->assign('permissions__'.$value,1);
		}
		$this->assign('is_project',$this->get_is_project());//得到我参与的项目权限
		$this->assign('is_project_user',$this->get_is_project_user());//得到项目组员权限
		$this->assign('is_project_function',$this->get_is_project_function());//得到授权给我的项目权限
		$this->assign('is_pbc',$this->get_is_pbc());//得到个人pbc权限
		$this->assign('is_task',$this->get_is_task());//得到个人任务管理权限
		header ("Content-Type:text/html; charset=utf-8");
	}

	//登录 designed by:zhangsen 2017-08-09
	function login(){
		if(cookie('username')){
			$this->assign('username',cookie('username'));
		}
		$this->display('Index/login');
	}

	//登录-->判断 designed by:zhangsen 2017-08-14
	function do_login(){
		$this->assign('username',cookie('username'));
		$username=$_POST['username'];
		$password=$_POST['password'];
        $goto = isset($_COOKIE['goto']) ? $_COOKIE['goto'] : 'http://' . $_SERVER['HTTP_HOST'] . __APP__ . "/Index/index";
		$db_user=M ('db_user'); //获取用户表
		if(!empty($username)){
			$map['regname']=$username;
			$map['mobilephone']=$username;
			$map['email']=$username;
			$map['_logic']='or';
			$where['_complex']=$map;
		}
		$user_ck=$db_user->where($where)->find(); //查询用户是否存在
		if(!empty($user_ck))
		{
			if($user_ck['status']==1)
			{
				$ip=$this->get_ip();
				if($password==$user_ck['password'] || !empty($ip))
				{
					$_SESSION['dms_uid']=$_SESSION['uid']=$user_ck['id'];
					$_SESSION['dms_username']=$_SESSION['username']=$user_ck['username'];
					$user=$this->get_user($_SESSION['dms_uid']);
					$_SESSION['dms_permissions_rule']=$_SESSION['permissions_rule']=$user['permissions_rule'];
					$_SESSION['dms_select_id']=$_SESSION['select_id']=explode(',',$user['select_id']);
					$_SESSION['dms_manage_id']=$_SESSION['manage_id']=explode(',',$user['manage_id']);
					$_SESSION['dms_sw_select_id']=$_SESSION['sw_select_id']=explode(',',$user['sw_select_id']);
					$_SESSION['dms_sw_manage_id']=$_SESSION['sw_manage_id']=explode(',',$user['sw_manage_id']);
					$_SESSION['dms_department']=$_SESSION['department']=$this->get_department($user['department_id']);
					$_SESSION['dms_department_id']=$_SESSION['department_id']=$user['department_id'];
					$_SESSION['dms_permissions_id']=$_SESSION['permissions_id']=$user['permissions_id'];
					$_SESSION['dms_center_id']=$_SESSION['center_id']=$user['center_id'];
					$_SESSION['dms_center_type']=$_SESSION['center_type']=$this->get_oa_dep_name($user['center_id']);
					if(isset($_SESSION['temp']['REQUEST_URI']))
					{
						$url=$_SESSION['temp']['REQUEST_URI'];
						unset($_SESSION['temp']);
					}
					else
					{
						$url=__APP__."/Index/index";
					}
					//cookie记住用户名
					cookie('username',$_POST['username'],60*60*24*365);
					$this->success('登录成功!',$goto);
				}else{
					$data['status']=self::NO;
					$data['msg']='帐号与密码不匹配,请重新输入';
					echo json_encode($data);die;
				}
			}
			else
			{
				$data['status']=self::NO;
				$data['msg']='该帐号已禁用';
				echo json_encode($data);die;
			}
		}
		else
		{
			$data['status']=self::NO;
			$data['msg']='帐号不存在';
			echo json_encode($data);die;
		}
	}

	//查出所有人员 designed by:zhanglijing 2016-12-05
	function get_all_username($status=1,$center_id=NULL,$special=NULL,$date=NULL,$uids=NULL){
		$db_user=M('db_user');
		if(empty($special)){$where['_string']="username!='admin'";}
		if(!empty($center_id)){$where['center_id']=$center_id;}
		if(!empty($uids)){$where['id']=array('in',$uids);}
		$where['status']=$status;
		if(!empty($date))
		{
			$where['add_date']=array('elt',$date);
		}
		$result=$db_user->field('id,username')->where($where)->order('spell')->select();
		return $result;
	}

	//查出所有员工号 designed by:zhanglijing 2013-10-29
	function get_user_number($status=NULL){
		$db_user=M('db_user');
		if(!empty($status)){$where['status']=$status;}
		$where['_string']="user_number is not NULL";
		$result=$db_user->field('id,username,user_number')->where($where)->order('spell')->select();
		return $result;
	}

	//查出所有部门 designed by:zhangsen 2017-09-01
	function get_all_department($uids=NULL){
		$db_user=M('db_user');
		$db_universal_label2=M('db_universal_label2');
		$where['department_id']=array('exp',' is not NULL');
		if(!empty($uids)){$where['id']=array('in',$uids);}
		$where['status']=1;
		$result=$db_user->group('department_id')->where($where)->order('department_id')->select();
		$where_user['status']=1;
		$where_user['id']=array('neq',1);
		foreach($result as $key=>$value)
		{
			$deps=$this->get_oa_dep_info($value['department_id']);
			$result[$key]['name']=$deps['HPS_DEPT_FULLDESCR'];
			$where_user['department_id']=$value['department_id'];
			if(!empty($uids)){$where_user['id']=array('in',$uids);}
			$result[$key]['user']=$db_user->field('id,username')->where($where_user)->order('spell')->select();
		}
		return $result;
	}

	//获取部门名称或id designed by:wangyitian 2017-07-03
	function get_department($department){
		$db_universal_label2=M('db_universal_label2');
		$where_p['id']=$department;
		$result=$db_universal_label2->where($where_p)->find();
		return $result;
	}

	//查出所有权限组；根据权限id查询某一个权限名称，没有id查询所有权限组名称 designed by:zhaoshihao 2016-10-13
	function get_permissions($permissions_id){
		$db_permissions=M('db_permissions');
		if(!empty($permissions_id))
		{
			$where['id']=$permissions_id;
			$result=$db_permissions->field('permissions_name')->where($where)->find();
			return $result['permissions_name'];
		}
		else
		{
			$result=$db_permissions->field('id,permissions_name')->where($where)->order('id asc')->select();
			foreach($result as $k=>$v)
			{
				if($v['permissions_name']=="超级管理员")
				{
					unset($result[$k]);
				}
			}
			return $result;
		}
	}

	
	//检查是否有权限 designed by:stefaniepei 2013-11-26
	function check_permission($num1,$num2,$num3){
		switch($num2){
			case 1:
				$num2=array('U1','U2','U3');
			break;
			case 2:
				$num2=array('U2','U3');
			break;
			case 3:
				$num2=array('U3');
			break;
			default:
				$num2=array('U1','U2','U3','U0');
			break;
		}
		switch($num3){
			case 1:
				$num3=array('Q1','Q2','Q3','Q6','Q7');
			break;
			case 2:
				$num3=array('Q2','Q3');
			break;
			case 3:
				$num3=array('Q3');
			break;
			case 4:
				$num3=array('Q0');
			break;
			case 5:
				$num3=array('QQ');
			break;
			case 6://add by chenyan 2018-09-25
				$num3=array('Q6');
			break;
			case 7://add by chenyan 2018-09-25
				$num3=array('Q7');
			break;
			case 8://add by chenyan 2018-09-25
				$num3=array('Q8');
			break;
			case 9://add by chenyan 2018-10-29-调整税率
				$num3=array('Q9');
			break;
			default:
				$num3=array('Q1','Q2','Q3','Q0','Q6','Q7','Q8','Q9');
			break;
		}
		$bool=0;
		foreach($num2 as $key=>$value)
		{
			foreach($num3 as $kk=>$vv)
			{
				if(preg_match("/".$num1.'_'.$value.'_'.$vv."/",$this->login_permissions_rule))
				{
					$bool=1;
					break;
				}
			}
		}
		if(!$bool)
		{
			$this->error('您没有对应权限!');die;
		}
	}

	//登录-->检查当前登录IP designed by:stefaniepei 2013-10-15
	function get_ip(){
		$db_ip=M('db_ip');
		$where['ip']=$_SERVER["REMOTE_ADDR"];
		$ip_result=$db_ip->field('id')->where($where)->find();
		if(!empty($ip_result))
		{
			return $ip_result;
		}
	}

	//检查 designed by:zhangsen 2017-02-20
	function bool_ip(){
		$ip=$_SERVER["REMOTE_ADDR"];
		if(!in_array(strtok($ip,'.'),array('10','127','192')))
		{
			return 0;
		}
		return 1;
	}

	//得到用户id designed by:zhanglijing 2014-05-05
	function get_uid($username){
		$db_user=M('db_user');
		$where['username']=$username;
		$uid=$db_user->field('id')->where($where)->find();
		return $uid['id'];
	}

	//获取用户username($array=1返回逗号连接的姓名,$array=2返回数组的姓名) designed by:zhaoshihao 2017-06-30
	function get_username($uid,$array=1){
		$db_user=M('db_user');
		$arrays=explode(',',$uid);
		foreach($arrays as $key=>$value){
			$where['id']=$value;
			$user=$db_user->field('username')->where($where)->find();
			$username[]=$user['username'];
		}
		if($array==1){
			$username=join(',',$username);
			return $username;
		}else{
			return $username;
		}
	}

	//获取用户详情 d$login_permissions_idesigned by:zhanglijing 2016-04-15
	function get_user($uid,$type=NULL){
		$db_user=M('db_user');
		if($type){
			$where['empl_id']=$uid;
		}else{
			$where['id']=$uid;
		}
		$result=$db_user->where($where)->find();
		$result['department']=$this->get_department($result['department_id']);
		//权限组
		$db_permissions=M('db_permissions');
		$where_p['id']=array('in',$result['permissions_id']);
		$p_result=$db_permissions->field('permissions_rule,select_id,manage_id,sw_select_id,sw_manage_id')->where($where_p)->select();
		foreach($p_result as $key=>$value){
			$str_p.=$value['permissions_rule'].',';
			if($value['select_id']){$str_s.=$value['select_id'].',';}
			if($value['manage_id']){$str_m.=$value['manage_id'].',';}
			if($value['sw_select_id']){$str_ss.=$value['sw_select_id'].',';}
			if($value['sw_manage_id']){$str_mm.=$value['sw_manage_id'].',';}
		}
		$result['permissions_rule']=rtrim($str_p,',');
		$result['select_id']=rtrim($str_s,',');
		$result['manage_id']=rtrim($str_m,',');
		$result['sw_select_id']=rtrim($str_ss,',');
		$result['sw_manage_id']=rtrim($str_mm,',');
		return $result;
	}

	//判断姓名是否重复，重复加职位
	function is_repeat_username($id){
		$db_user=M('db_user');
		$where_s['id']=$id;
		$uid=$db_user->where($where_s)->find();
		$where['username']=$uid['username'];
		$users=$db_user->where($where)->select();
		if(count($users)>1){
			$zw=$this->get_user($id);
			if(!empty($zw['repeat_name'])){
				$user=$uid['username'].'('.$zw['repeat_name'].')';
			}else{
				$user=$uid['username'];
			}
			return $user;
		}else{
			return $uid['username'];
		}
	}

	//搜库列表筛选 (参数1:包含全部) designed by:zhaoshihao 2016-11-09
	function library_type_list($classif,$fid=NULL,$sid=NULL,$all=NULL,$fcenter_id=NULL){
		$db_universal_label1=M('db_universal_label1');
		$where['first_name']=array('exp',' is not NULL');
		$where['classif']=$classif;
		if(!empty($fid)){$where['id']=$fid;}
		if(!empty($fcenter_id)){$where['fcenter_id']=$fcenter_id;}
		$result=$db_universal_label1->field('id,first_name,property')->where($where)->order('first_sort')->select();
		$db_universal_label2=M('db_universal_label2');
		$db_universal_label3=M('db_universal_label3');
		$where_type['type_id']=1;
		foreach($result as $k=>$v){
			if(!empty($all)){$where_s['second_name']=array('neq','全部');}
			$where_s['first_id']=$v['id'];
			$result[$k]['second']=$db_universal_label2->field('id,second_name')->where($where_s)->order('second_sort')->select();
			foreach($result[$k]['second'] as $k1=>$v1){
				if(!empty($sid)){
					if(in_array($v1['id'],explode(',',$sid))){
						$where_s['second_id']=$v1['id'];
						$result[$k]['second'][$k1]['third']=$db_universal_label3->field('id,third_name')->where($where_s)->order('third_sort')->select();
					}
				}else{
					$where_s['second_id']=$v1['id'];
					$result[$k]['second'][$k1]['third']=$db_universal_label3->field('id,third_name')->where($where_s)->order('third_sort')->select();
				}
			}
		}
		return $result;
	}

	//降维(数组,字段) designed by:zhaoshihao 2016-05-12
	function kg_array_values($array, $field){
		if (empty($array) || !is_array($array))
		{
			return null;
		}
		$result = array();
		foreach ($array as $value)
		{
			if (isset($value[$field]))
			{
				$result[] = $value[$field];
			}
		}
		return $result;
	}

	//得到周几 designed by:zhanglijing 2016-07-18
	function get_day_week($date){
		$db_periodic=M('db_periodic');
		$where['db_date']=$date;
		$result=$db_periodic->field('day_of_week')->where($where)->find();
		return $result['day_of_week'];
	}

	//年/月/周相关数据 designed by:zhanglijing 2015-09-18
	function get_year_month_week($type=1,$year_now,$week_now){
		if($type==1)
		{//去年,今年,明年,后年的数组
			$year[]=$this->get_date("Y","+2 year");
			$year[]=$this->get_date("Y","+1 year");
			$year[]=date('Y');
			$year[]=$this->get_date("Y","-1 year");
			return $year;
		}
		elseif($type==2)
		{//12个月,01,02,03,04,05,06,07,08,09,10,11,12的数组
			$month[]='01';
			$month[]='02';
			$month[]='03';
			$month[]='04';
			$month[]='05';
			$month[]='06';
			$month[]='07';
			$month[]='08';
			$month[]='09';
			$month[]='10';
			$month[]='11';
			$month[]='12';
			return $month;
		}
		elseif($type==3)
		{//12个月,1,2,3,4,5,6,7,8,9,10,11,12的数组
			$month[]='1';
			$month[]='2';
			$month[]='3';
			$month[]='4';
			$month[]='5';
			$month[]='6';
			$month[]='7';
			$month[]='8';
			$month[]='9';
			$month[]='10';
			$month[]='11';
			$month[]='12';
			return $month;
		}
		elseif($type==4)
		{//所有周的数组
			$big_week=$this->get_date("W",NULL,$year_now.'-12-31');
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-30');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-29');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-28');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-27');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-26');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-25');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-24');}
			for($i=1;$i<=$big_week;$i++)
			{
				$week[]=$i;
			}
			return $week;
		}
		elseif($type==5)
		{//最大周数
			$big_week=$this->get_date("W",NULL,$year_now.'-12-31');
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-30');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-29');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-28');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-27');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-26');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-25');}
			if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-24');}
			return $big_week;
		}
		elseif($type==6)
		{//指定年和周的日期数组
			$time=new \DateTime();
			$time->setISODate($year_now,$week_now,1);
			$result[]=$time->format('Y-m-d');
			$time->setISODate($year_now,$week_now,2);
			$result[]=$time->format('Y-m-d');
			$time->setISODate($year_now,$week_now,3);
			$result[]=$time->format('Y-m-d');
			$time->setISODate($year_now,$week_now,4);
			$result[]=$time->format('Y-m-d');
			$time->setISODate($year_now,$week_now,5);
			$result[]=$time->format('Y-m-d');
			$time->setISODate($year_now,$week_now,6);
			$result[]=$time->format('Y-m-d');
			$time->setISODate($year_now,$week_now,7);
			$result[]=$time->format('Y-m-d');
			return $result;
		}
		elseif($type==7)
		{//2011年到明年的年份
			for($i=2011;$i<=$this->get_date("Y","+1 year");$i++){
				$year[]=$i;
			}
			return $year;
		}
	}

	//获得节假日 designed by:zhanglijing 2015-09-18
	function get_holiday($year,$name){
		$db_holiday=M('db_holiday');
		$where['year']=$year;
		$where['name']=$name;
		$result=$db_holiday->where($where)->find();
		return $result;
	}
	//获得所有节假日 designed by:zhanglijing 2015-09-18
	function get_holiday_all($year){
		$db_holiday=M('db_holiday');
		$where['year']=$year;
		$result=$db_holiday->where($where)->order('week')->select();
		return $result;
	}

	//得到日期加减操作(参数:$return返回日期类型,$string加减逻辑字符串(例如："+1 years","+1 month","+1 day"),$date指定日期值)designed by:zhangsen 2017-04-28
	/*
	用法:
	$this->get_date("Y-m-d","+1 day");当前日期加一天
	$this->get_date("Y-m-d","+1 month",$date);指定日期下一月
	$this->get_date("Y-m","+1 years",$date);指定日期下一年的年月
	$this->get_date("Y-m-t",NULL,$date);指定日期的月底
	$this->get_date("m","+1 month",$date);指定日期下一个月的月份(获取月份时候，日期必填。不然个别日期会有bug存在)
	等等，根据需求自己获取。代码里面strtotime获取日期的地方都会变成公共方法调用。
	*/
	function get_date($return,$string,$date){
		if(empty($return)){
			$return="Y-m-d";
		}
		if(empty($string)){
			$string="now";
		}
		if(empty($date)){
			$date = time();
		}else{
			$date = strtotime(date($date));
		}
		if(preg_match("/month/",$string))
		{
			$first_day_of_month = date('Y-m',$date).'-01';
			$str_date = strtotime($first_day_of_month);
		}
		if(preg_match("/month/",$string))
		{
			$l_date=date("Y-m-d",strtotime($string,$str_date));
			$day=substr(date("Y-m-d",$date),8,2);
			if(substr($l_date,0,7).'-'.date('t',strtotime(substr($l_date,0,7)))<substr($l_date,0,7).'-'.$day){
				$l_date=substr($l_date,0,7).'-'.date('t',strtotime(substr($l_date,0,7)));
			}else{
				$l_date=substr($l_date,0,7).'-'.$day;
			}
			$l_date=date($return,strtotime($l_date));
			return $l_date;
		}else{
			return date($return,strtotime($string,$date));
		}
	}

	//汉字字符串截取 designed by:wangyitian 2017-08-03
	function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
	{
		if(function_exists("mb_substr"))
		{
			if($suffix)
			{
				return mb_substr($str, $start, $length, $charset)."...";
			}
			else
			{
				return mb_substr($str, $start, $length, $charset);
			}
		}
		elseif(function_exists('iconv_substr'))
		{
			if($suffix)
			{
				return iconv_substr($str,$start,$length,$charset)."...";
			}
			else
			{
				return iconv_substr($str,$start,$length,$charset);
			}
		}
		$re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
		$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
		$re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
		$re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
		preg_match_all($re[$charset], $str, $match);
		$slice = join("",array_slice($match[0], $start, $length));
		if($suffix)
		{
			return $slice."...";
		}
		else
		{
			return $slice;
		}
	}

	//获取公共类别表数据(参数说明:$type/$name1/$name2/$name3/$name4/$id:表字段,$ftype:查询使用select/find返回一维/二维数组,$stype:逗号分割数据返回数组) designed by:wsangyitian 2017-02-16
	function get_db_universal_type($type=NULL,$ftype=NULL,$stype=NULL,$main=NULL,$name1=NULL,$name2=NULL,$name3=NULL,$name4=NULL,$id=NULL){
		$db_universal_type=M('db_universal_type');
		if(!empty($type)){$where['type']=$type;}
		if(!empty($name1)){$where['name1']=$name1;}
		if(!empty($name2)){$where['name2']=$name2;}
		if(!empty($name3)){
			if($type==6){
				$where['name3']=array('in',$name3);
			}else{
				$where['name3']=$name3;
			}
		}
		if(!empty($name4)){$where['name4']=$name4;}
		if(!empty($id)){$where['id']=$id;}
		if(!empty($ftype)){
			$result=$db_universal_type->where($where)->find();
		}else{
			$result=$db_universal_type->where($where)->order('id')->select();
		}
		if(!empty($stype)){
			$name_arr1=explode(',',$result['name1']);
			if(!empty($result['name2'])){$name_arr2=explode(',',$result['name2']);}
			unset($result);
			foreach($name_arr1 as $key=>$value){
				$result[$key]['name1']=$value;
			}
			if(!empty($name_arr2))
			{
				foreach($name_arr2 as $key=>$value){
					$result[$key]['name2']=$value;
				}
			}
		}
		if(!empty($main)){
			foreach($result as $key=>$value){
				if($value['name2']==1000 || $value['name1']==-10){
					unset($result[$key]);
				}
			}
		}
		return $result;
	}

	//获取标签表数据 designed by:zhangsen 2017-09-16
	function get_db_universal_label($type=NULL,$id=NULL){
		$db_universal_label1=M('db_universal_label1');
		$db_universal_label2=M('db_universal_label2');
		$db_universal_label3=M('db_universal_label3');
		$where['id']=$id;
		if($type==1){
			$result_arr=$db_universal_label1->where($where)->find();
			$result=$result_arr['first_name'];
		}elseif($type==2){
			$result_arr=$db_universal_label2->where($where)->find();
			$result=$result_arr['second_name'];
		}elseif($type==3){
			$result_arr=$db_universal_label3->where($where)->find();
			$result=$result_arr['third_name'];
		}
		return $result;
	}

	//获取一级表的所有二级 designed by:zhangsen 2017-09-16
	function get_db_universal_label2($classif=NULL,$fid=NULL){
		if($_SERVER['SERVER_ADDR']=='192.168.0.7'){
			$jc_universal_label1= M('jc_universal_label1','','mysql://root:xxjs666weigang@192.168.0.4/jc_erp'); //数据库测试
			$jc_universal_label2= M('jc_universal_label2','','mysql://root:xxjs666weigang@192.168.0.4/jc_erp'); //数据库测试
		}else{
			$jc_universal_label1= M('jc_universal_label1','','mysql://root:@127.0.0.1/jc_erp'); //数据库测试
			$jc_universal_label2= M('jc_universal_label2','','mysql://root:@127.0.0.1/jc_erp'); //数据库测试
		}
		$where['classif']=$classif;
		if(!empty($fid)){$where['id']=$fid;}
		$class=$jc_universal_label1->where($where)->select();
		foreach($class as $key=>$val){
			if($key==0){$cid=$val['id'];}else{$cid.=','.$val['id'];}
		}
		$where_s['first_id']=array('in',"$cid");
		$where_s['second_name']=array('neq','全部');
		$result_arr=$jc_universal_label2->where($where_s)->select();
		return $result_arr;
	}

	//获取项目信息 designed by:zhangsen 2017-09-15
	function get_project($pid){
		$db_project=M('db_project');
		$where['id']=$pid;
		$project=$db_project->where($where)->find();
		return $project;
	}

	//得到项目启动表 designed by:zhanglijing 2013-11-14
	function get_project_bg($pid){
		$db_project_bg=M('db_project_bg');
		$where['pid']=$pid;
		$project_bg=$db_project_bg->where($where)->find();
		return $project_bg;
	}

	//得到省市信息(type=1:得到省市;type=2:省市id转名字;type=3:省市名字转id,$bool:返回类型)designed by:wangyitian 2017-05-11
	function get_province_city($region,$type=1,$bool=NULL){
		$db_province_city=M('db_province_city');
		if($type==1){
			if($region==NULL){$where['reid']=0;}else{$where['reid']=$region;}
			$result=$db_province_city->where($where)->select();
		}elseif($type==2){
			if($region!=NULL){$where['id']=$region;}
			$list=$db_province_city->where($where)->find();
			$result=$list['name'];
		}elseif($type==3){
			if($region!=NULL){$where['name']=$region;}
			$list=$db_province_city->where($where)->find();
			$result=$list['id'];
		}
		if($bool==NULL){
			return $result;
		}else{
			echo json_encode($result);
		}
	}

	//操作日志
	function run_log(){
		$db_run_log=M('db_run_log');
        $data['uid']=$this->login_id;
        $data['username']=$this->login_username;
		$data['url']=substr($_SERVER["REQUEST_URI"],strpos($_SERVER["REQUEST_URI"],'index.php')+strlen('index.php?s='));
		$data['ip']=$_SERVER["REMOTE_ADDR"];
        $data['add_date']=date("Y-m-d H:i:s");
        $db_run_log->add($data);
	}

	//我参与的项目权限 designed by:zhangsen 2017-09-29
	function get_is_project(){
		$db_project=M('db_project');
		$where['_string']="(manager_uid=".$this->login_id.") or (director_uid=".$this->login_id.")";
		$count=$db_project->field('db_project.id')->where($where)->count();
		if($count>0){
			return 1;
		}
	}

	//我参与的项目组员权限 designed by:zhangsen 2017-09-29
	function get_is_project_user(){
		$db_project=M('db_project');
		$where['_string']='find_in_set("'.$this->login_id.'",project_uids)';
		$count=$db_project->field('db_project.id')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->where($where)->count();
		$t_day=date('Y-m-d');
		$str="(SELECT db_project.project_num,db_project.project_name,db_project.status,db_project_user.pid,db_project_user.uid,db_project_user.nature,db_project_user.come_date FROM `db_project` inner join db_project_user on db_project.id=db_project_user.pid LEFT JOIN db_user on db_project_user.uid=db_user.id WHERE ( db_project.`center_id` = 130005 ) AND (('".$t_day."'>=come_date and '".$t_day."'<`out_date`) or ('".$t_day."'>=come_date and out_date is NULL)) and db_user.is_project=1 and db_user.status=1 and db_project_user.uid='".$this->login_id."' group by pid) ORDER BY project_num desc,come_date desc";
		$Model=M();
		$result=$Model->query($str);
		if($count+count($result)>0){
			return 1;
		}
	}

	//项目功能权限 designed by:zhangsen 2017-10-24
	function get_is_project_function(){
		$db_project_function=M('db_project_function');
		$where['uid']=$this->login_id;
		$where['start_date']=array('elt',date("Y-m-d"));
		$where['end_date']=array('egt',date("Y-m-d"));
		$count=$db_project_function->where($where)->count();
		if($count>0){
			return 1;
		}
	}

	//PBC个人权限 designed by:zhangsen 2018-03-19
	function get_is_pbc(){
		$db_jixiao_pbc_user=M('db_jixiao_pbc_user');
		$where['u_id']=$this->login_id;
		$where['status']=1;
		$count=$db_jixiao_pbc_user->where($where)->count();
		if($count>0){
			return 1;
		}
	}

	//个人任务管理权限 designed by:zhangsen 2018-03-26
	function get_is_task(){
		$db_task=M('db_task');
		$db_task_detail=M('db_task_detail');
		$where_d['uid']=$this->login_id;
		$result_d=$db_task_detail->field('mid')->where($where_d)->select();
		foreach($result_d as $key=>$value){
			$mid_str.=$value['mid'].',';
		}
		if($mid_str){
			$where['_string']="db_task.id in (".rtrim($mid_str,',').") or db_task.head_uid='".$this->login_id."' or db_audit_main.add_uid='".$this->login_id."'";
		}
		else{
			$where['_string']=" db_task.head_uid='".$this->login_id."' or db_audit_main.add_uid='".$this->login_id."'";
		}
		$count=$db_task->join('db_audit_main on db_task.id=db_audit_main.audit_id')->where($where)->count();
		if($count>0){
			return 1;
		}
	}

	//审核流程-下一步审核判断 designed by:zhangsen 2016-08-12
	function audit_type_next_person($type,$audit_type,$status,$sort){
		$db_universal_audit=M('db_universal_audit');
		$where['type']=$type;
		if(!empty($audit_type)){$where['audit_type']=$audit_type;}
		$where['audit_uid']=array('exp','IS NOT NULL');
		if($sort=='egt'){
			$where['audit_sort']=array('egt',$status);
			$result=$db_universal_audit->where($where)->order('audit_sort asc')->find();
		}
		elseif($sort=='lt'){
			$where['audit_sort']=array('lt',$status);
			$result=$db_universal_audit->where($where)->order('audit_sort desc')->find();
		}
		else{
			$where['audit_sort']=$status;
			$result=$db_universal_audit->where($where)->order('audit_sort')->find();
		}
		return $result;
	}

	//审核流程-审核对应分组 designed by:zhaoshihao 2016-07-27
	function audit_user($type,$audit_type=NULL,$audit_name=NULL){
		$db_universal_audit=M('db_universal_audit');
		$where['type']=$type;
		if(!empty($audit_type)){$where['audit_type']=$audit_type;}
		if(!empty($audit_name)){$where['audit_name']=$audit_name;}
		$result=$db_universal_audit->where($where)->order('audit_sort')->select();
		return $result;
	}

	//营销计划-->审核 designed by:zhangsen 2016-08-17
	function apply_audit_next_judge($type,$audit_type,$status,$sort){
		$audit_next=$this->audit_type_next($type,$audit_type,$status,$sort);
		//根据状态各种判断
		if($sort=='gt'){
			if(empty($audit_next['audit_sort'])){
				$audit_next['audit_sort']=4;
			}
		}
		elseif($sort=='lt'){
			if(empty($audit_next['audit_sort'])){
				if($status==2){
					$audit_next['audit_sort']=1;
				}
				else{
					$audit_next['audit_sort']=0;
				}
			}
		}
		return $audit_next;
	}

	//是否是项目经理 designed by:stefaniepei 2014-06-12
	function bool_project_manager(){
		$db_project=M('db_project');
		$where['manager_uid']=$this->login_id;
		$project_result=$db_project->field('id')->where($where)->find();
		if(!empty($project_result)){
			return 1;
		}
		return 0;
	}

	//得到登录人可以审核的类型 designed by:zhaoshihao 2016-08-08
	function get_audit_users($type,$audit_type=NULL,$audit_name,$uid){
		$db_universal_audit=M('db_universal_audit');
		$where['type']=$type;
		if(!empty($audit_type)){$where['audit_type']=$audit_type;}
		$where['audit_name']=$audit_name;
		$where['_string']='find_in_set("'.$uid.'",audit_uid)';
		$job_result=$db_universal_audit->where($where)->select();
		foreach($job_result as $key=>$value){
			if($key!=0){$user_arr.=','.$value['audit_type'];}else{$user_arr.=$value['audit_type'];}
		}
		return $user_arr;
	}

	//获取审核流程--designed by:wangyitian 2017-02-20
	function get_audit_process($type,$bool=NULL){
		$db_universal_audit=M('db_universal_audit');
		$where['type']=$type;
		$result=$db_universal_audit->where($where)->group('audit_name')->order('audit_sort')->select();
		if(empty($bool))
		{
			$where['audit_uid']=array('exp',' is not NULL');
			$where['audit_user']=array('exp',' is not NULL');
		}
		foreach($result as $key=>$value)
		{
			$where['audit_name']=$value['audit_name'];
			$bool=$db_universal_audit->where($where)->find();
			if(empty($bool))
			{
				unset($result[$key]);
			}
		}
		return array_values($result);
	}

	//审核流程-下一步审核判断 designed by:zhangsen 2016-08-12
	function audit_type_next($type,$audit_type,$status,$sort){
		$db_universal_audit=M('db_universal_audit');
		$where['type']=$type;
		if(!empty($audit_type)){$where['audit_type']=$audit_type;}
		if($sort=='gt'){
			$where['audit_sort']=array('gt',$status);
			$order='audit_sort';
		}
		elseif($sort=='lt'){
			$where['audit_sort']=array('lt',$status);
			$order='audit_sort desc';
		}
		else{
			$where['audit_sort']=$status;
		}
		$where['audit_uid']=array('exp','IS NOT NULL');
		$result=$db_universal_audit->where($where)->order($order)->find();
		return $result;
	}

	//审核流程列表-是否对应审核人 designed by:zhangsen 2016-08-07
	function bool_audit_list_user($type,$audit_name){
		$db_universal_audit=M('db_universal_audit');
		$where['type']=$type;
		$where['audit_name']=$audit_name;
		$where['_string']='find_in_set("'.$this->login_id.'",audit_uid)';
		$result=$db_universal_audit->where($where)->find();
		if($result)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

	//数组重新排序 multi_array_sort(数组，键名，排序方式) designed by:stefaniepei 2013-10-15
	function multi_array_sort($multi_array,$sort_key,$sort=SORT_DESC){
		if(is_array($multi_array)){
			foreach ($multi_array as $row_array){
				if(is_array($row_array)){
					$key_array[]=$row_array[$sort_key];
				}else{
					return false;
				}
			}
		}else{
			return false;
		}
		array_multisort($key_array,$sort,$multi_array);
		return $multi_array;
	}

	//随机颜色代码 designed by:zhaoshihao 2017-11-20
	function randColor($num){
		//color value limits 0-16777215;
		$prehash='#';    //using string '#' before the color value;
		$rsArr= array();
		for($i=0;$i<$num;$i++){
		$color=rand(0,16777215);
		$rsArr[$i]=$prehash.dechex($color);
		}
		return $rsArr;
	}

	//获取一个月天数的数组 designed by:zhaoshihao 2017-12-05
	function get_month_days($year,$month){
		//一个月有多少天
		$days_num=$this->get_date("t",NULL,$year.'-'.$month);
		//一号是周几
		$week=$this->get_date("w",NULL,$year.'-'.$month.'-01');
		//补上前面的空格
		if(($week>1) or ($week==0)){$days[]['d']=0;}
		if(($week>2) or ($week==0)){$days[]['d']=0;}
		if(($week>3) or ($week==0)){$days[]['d']=0;}
		if(($week>4) or ($week==0)){$days[]['d']=0;}
		if(($week>5) or ($week==0)){$days[]['d']=0;}
		if($week==0){$days[]['d']=0;}
		//一个月天数放进数组
		for($d=1;$d<=$days_num;$d++)
		{
			$days[]['d']=sprintf("%02d",$d);
		}
		//后面还差多少个格子
		$rows=ceil(count($days)/7)*7-count($days);
		//补上后面的空格
		if($rows>0)
		{
			for($r=1;$r<=$rows;$r++)
			{
				$days[]['d']=0;
			}
		}
		//7个格子一换行
		foreach($days as $k=>$v)
		{
			if((is_int(($k+1)/7)) and ($k!=(count($days)-1)))
			{
				$days[$k]['br']=1;
			}
		}
		return $days;
	}

	//发送提醒 designed by:zhanglijing 2015-11-17
	function alert_send($alert_type,$to_users_array,$remark=NULL,$status=-1,$lid=NULL,$add_user=NULL,$add_uid=NULL){
		//提醒类型
		$db_alert_type=M('db_alert_type');
		$where_t['alert_type']=$alert_type;
		$result_t=$db_alert_type->field('id,type')->where($where_t)->find();
		//发送提醒
		$db_alert=M('db_alert');
		foreach($to_users_array as $key=>$value)
		{
			$where['alert_type_id']=$result_t['id'];
			if(!empty($lid))
			{
				$where['lid']=$lid;
			}
			$where['to_uid']=$value['id'];
			$where['to_user']=$value['username'];
			if(empty($add_uid))
			{
				$where['add_uid']=$this->login_id;}
			else
			{
				$where['add_uid']=$add_uid;
			}
			if(empty($add_user))
			{
				$where['add_user']=$this->login_username;
			}
			else
			{
				$where['add_user']=$add_user;
			}
			$where['add_date']=date('Y-m-d H:i:s');
			$where['status']=$status;
			$where['type']=$result_t['type'];
			$where['remark']=$remark;
			$db_alert->add($where);
		}
	}

	//公共方法-->已读提醒(单条) designed by:zhanglijing 2014-03-19
	function alert_yidu($alert_type,$leave_main_id,$uid=NULL){
		$db_alert_type=M('db_alert_type');
		$a_t_where['alert_type']=$alert_type;
		$id=$db_alert_type->field('id')->where($a_t_where)->find();//查询提醒类型的ID

		$db_alert=M('db_alert');
		$where_main['alert_type_id']=$id['id'];
		$where_main['lid']=$leave_main_id;
		if(!empty($uid))
		{
			$where_main['to_uid']=$uid;
		}
		$main_id=$db_alert->field('id')->where($where_main)->select();//查询提醒的ID
		foreach($main_id as $key=>$value)
		{
			$save_main['id']=$value['id'];
			$save_main['status']=1;
			$db_alert->save($save_main);
		}
	}

	//公共方法-->已读提醒(多条) designed by:zhanglijing 2015-11-20
	function alert_yidu_all($lid){
		$db_alert=M('db_alert');
		$where['lid']=$lid;
		$result=$db_alert->field('id')->where($where)->select();
		foreach($result as $key=>$value)
		{
			$where_save['id']=$value['id'];
			$where_save['status']=1;
			$db_alert->save($where_save);
		}
	}

	//获取未读提醒数量 designed by:zhanglijing 2015-11-20
	function get_alert_num(){
		//提醒
		$db_alert=M('db_alert');
		$where['status']=-1;
		$where['to_uid']=$this->login_id;
		$where['to_user']=$this->login_username;
		$result['al_ct']=$db_alert->field('db_alert.id,content,url,status,add_date,remark,to_uid,add_user,db_alert.type')->join('LEFT JOIN db_alert_type ON db_alert_type.id=db_alert.alert_type_id')->where($where)->count();
		//被分享
		$db_library_share=M('db_library_share');
		$where_sh['suid']=$this->login_id;
		$where_sh['rstatus']=-1;
		$share=$db_library_share->field('db_library_share.id')->where($where_sh)->join('LEFT JOIN db_library_share_starus ON db_library_share.id=db_library_share_starus.sid')->order('share_date desc')->group('db_library_share.id')->select();
		$result['bfx']=count($share);
		//分享
		$db_library_share_starus=M('db_library_share_starus');
		$where['add_uid']=$this->login_id;
		$share_fx=$db_library_share->field('id')->where($where)->select();
		foreach($share_fx as $ks=>$vs){
			$where_st['sid']=$vs['id'];
			$asd=$db_library_share_starus->where($where_st)->find();
			if(empty($asd)){unset($share_fx[$ks]);}
		}
		$result['fx']=count($share_fx);
		//echo $db_alert->getLastSql();die;
		return $result;
	}

	//用户列表-->审核/审批关系 designed by:zhangsen 2018-05-08
	function user_list_ajax_relationship($type,$uid,$status=NULL,$leader_uid=NULL){
		//用户详情
		$user=$this->get_user($uid);
		$db_relationship=M('db_relationship');
		if($type==1)
		{
			//查出此用户的审核/审批人
			$where_relationship['_string']='find_in_set("'.$uid.'",person_uid)';
			$relationship=$db_relationship->where($where_relationship)->select();
			if(!empty($relationship))
			{
				foreach($relationship as $key=>$value)
				{
					if($value['status']==1)
					{
						$result['audit_uid']=$value['leader_uid'];
						$result['audit_user']=$value['leader_name'];
					}
					else
					{
						$result['approval_uid']=$value['leader_uid'];
						$result['approval_user']=$value['leader_name'];
					}
				}
				return $result;
			}
		}
		elseif($type==2)
		{
			//添加用户的审核/审批人
			$where_relationship['leader_uid']=$leader_uid;
			$where_relationship['status']=$status;
			$relationship=$db_relationship->where($where_relationship)->find();
			if(!empty($relationship))
			{
				$where_save['id']=$relationship['id'];
				$where_save['person_uid']=$relationship['person_uid'].','.$uid;
				$where_save['person_name']=$relationship['person_name'].','.$user['username'];
				$db_relationship->save($where_save);
			}
			else
			{
				$where_add['leader_uid']=$leader_uid;
				$leader_name=$this->get_user($leader_uid);
				$where_add['leader_name']=$leader_name['username'];
				$where_add['person_uid']=$uid;
				$where_add['person_name']=$user['username'];
				$where_add['status']=$status;
				$db_relationship->add($where_add);
			}
		}
		elseif($type==3)
		{
			//删除用户的审核/审批人
			$where_relationship['leader_uid']=$leader_uid;
			$where_relationship['status']=$status;
			$relationship=$db_relationship->where($where_relationship)->find();
			$person_uid=explode(',',$relationship['person_uid']);
			if(count($person_uid)==1)
			{
				$where_delete['id']=$relationship['id'];
				$db_relationship->where($where_delete)->delete();
			}
			else
			{
				foreach($person_uid as $key=>$value)
				{
					if($value!=$uid)
					{
						$person_uid_array[]=$value;
					}
				}
				$person_name=explode(',',$relationship['person_name']);
				foreach($person_name as $key=>$value)
				{
					if($value!=$user['username'])
					{
						$person_name_array[]=$value;
					}
				}
				$where_save['id']=$relationship['id'];
				$where_save['person_uid']=implode(',',$person_uid_array);
				$where_save['person_name']=implode(',',$person_name_array);
				$db_relationship->save($where_save);
			}
		}
		elseif($type==4)
		{
			//查询此用户需要审核/审批的人员
			$where_relationship['leader_uid']=$uid;
			$where_relationship['status']=$status;
			$relationship=$db_relationship->field('person_uid')->where($where_relationship)->find();
			return $relationship['person_uid'];
		}
	}

	function get_oa_dep($date,$time){
		header('Content-Type: text/html; charset=utf-8');
		$xmldata = <<<EOT
		<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hps="http://xmlns.oracle.com/Enterprise/Tools/schemas/HPS_HRIFOA_DEP_REQ.V1">
		   <soapenv:Header/>
		   <soapenv:Body>
			  <hps:HPS_HRIFOA_DEP_REQ>
				 <hps:MsgData>
					<hps:Transaction>
					   <hps:HPS_INF_REQ class="R">
						  <hps:HPS_IFREQ_DT IsChanged="?">$date</hps:HPS_IFREQ_DT>
						  <hps:HPS_IFREQ_DTTM IsChanged="?">$time</hps:HPS_IFREQ_DTTM>
					   </hps:HPS_INF_REQ>
					</hps:Transaction>
				 </hps:MsgData>
			  </hps:HPS_HRIFOA_DEP_REQ>
		   </soapenv:Body>
		</soapenv:Envelope>
EOT;
		$wsdl='http://10.1.0.105/PSIGW/PeopleSoftServiceListeningConnector/PSFT_HR/HPS_HRIF001_OA_DEPT.1.wsdl';
        $streamContext = array(stream_context_create(array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false
            )
        )));
        $client = new \SoapClient($wsdl, $streamContext);
		$result=$client->__doRequest($xmldata,$wsdl,'HPS_HFIFOA_DEP_SRVACTN.v1',1,0);
		return $result;
	}

	function get_oa_user($date,$time){
		header('Content-Type: text/html; charset=utf-8');
		$xmldata = <<<EOT
		<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hps="http://xmlns.oracle.com/Enterprise/Tools/schemas/HPS_HRIFOA_EMP_REQ.V1">
		   <soapenv:Header/>
		   <soapenv:Body>
			  <hps:HPS_HRIFOA_EMP_REQ>
				 <hps:MsgData>
					<hps:Transaction>
					   <hps:HPS_INF_REQ class="R">
						  <hps:HPS_IFREQ_DT IsChanged="?">$date</hps:HPS_IFREQ_DT>
						  <hps:HPS_IFREQ_DTTM IsChanged="?">$time</hps:HPS_IFREQ_DTTM>
					   </hps:HPS_INF_REQ>
					</hps:Transaction>
				 </hps:MsgData>
			  </hps:HPS_HRIFOA_EMP_REQ>
		   </soapenv:Body>
		</soapenv:Envelope>
EOT;
		$wsdl='http://10.1.0.105/PSIGW/PeopleSoftServiceListeningConnector/PSFT_HR/HPS_HRIF001_OA_EMPL.1.wsdl';
        /*$streamContext = array(stream_context_create(array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false
            )
        )));*/
        $opts = array(
            'ssl' => array('ciphers'=>'RC4-SHA', 'verify_peer'=>false, 'verify_peer_name'=>false)
        );
        $params = array ('encoding' => 'UTF-8', 'soap_version' => SOAP_1_2, 'trace' => 1, 'exceptions' => 1, "connection_timeout" => 180, 'stream_context' => stream_context_create($opts) );
        $client = new \SoapClient($wsdl, $params);
		$result=$client->__doRequest($xmldata,$wsdl,'HPS_HFIFOA_EMP_SRVACTN.v1',1,0);
		return $result;
	}

	function get_oa_dep_next($dept_id){
		$db_department=M('db_department');
		$where['DEPTID']=$dept_id;
		$dep_oa=$db_department->where($where)->find();
		if($dep_oa['PART_DEPTID_CHN']=="130001"){
			$result=$dep_oa;
		}else{
			$where_a['DEPTID']=$dep_oa['PART_DEPTID_CHN'];
			$dep_oa_a=$db_department->where($where_a)->find();
			if($dep_oa_a['PART_DEPTID_CHN']=="130001"){
				$result=$dep_oa_a;
			}else{
				$where_b['DEPTID']=$dep_oa_a['PART_DEPTID_CHN'];
				$dep_oa_b=$db_department->where($where_b)->find();
				if($dep_oa_b['PART_DEPTID_CHN']=="130001"){
					$result=$dep_oa_b;
				}
			}
		}
		return $result;
	}

	function get_oa_dep_info($dept_id){
		$db_department=M('db_department');
		$where['DEPTID']=$dept_id;
		$where['EFF_STATUS']='A';
		$result=$db_department->where($where)->find();
		return $result;
	}

	function get_oa_dep_list(){
		$db_department=M('db_department');
		$where['PART_DEPTID_CHN']=130001;
		$where['EFF_STATUS']='A';
		$result=$db_department->field('DEPTID as id,DEPT_DESCR as first_name')->where($where)->order('SORT')->select();
		return $result;
	}

	function get_oa_dep_name($id){
		$db_department=M('db_department');
		$where['DEPTID']=$id;
		$result=$db_department->where($where)->find();
		return $result['DEPT_DESCR'];
	}

	//curl_post发送请求 designed by:zhangsen 2018-05-17
	function curl_post($url,$data=array()){
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
		//POST数据
		curl_setopt($ch, CURLOPT_POST,1);
		//把post的变量加上
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		$output=curl_exec($ch);
		curl_close($ch);
		return $output;
	}

	//微信发送审核提醒 designed by:zhangsen 2018-05-17
	function send_audit_notice($openid,$url,$first,$keyword1,$keyword2,$keyword3,$keyword4,$remark){
		$ip=$this->get_ip();
		if(empty($ip) || $_SERVER['SERVER_ADDR']=='192.168.0.7')
		{
			//获取access_token
			if($_COOKIE['access_token']){
				$access_token2=$_COOKIE['access_token'];
			}else{
				$json_token=$this->curl_post("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa9104bb1d0267056&secret=1e77aa81ba98a0f0fd8699dc41d1ad87");
				$access_token1=json_decode($json_token,true);
				$access_token2=$access_token1['access_token'];
				setcookie('access_token',$access_token2,7200);
			}
			//模板消息
			$template=array(
				'touser'=>$openid,//用户openid
				'template_id'=>"hNulYpuefkRz4A_ODtr14cxMkHGZ5_2r8sN0gzqQMco",//在公众号下配置的模板id
				'url'=>$url,//点击模板消息会跳转的链接
				'topcolor'=>"#7B68EE",
				'data'=>array(
					'first'=>array('value'=>urlencode($first),'color'=>"#FF0000"),
					'keyword1'=>array('value'=>urlencode($keyword1),'color'=>'#FF0000'),//keyword需要与配置的模板消息对应
					'keyword2'=>array('value'=>urlencode($keyword2),'color'=>'#FF0000'),
					'keyword3'=>array('value'=>urlencode($keyword3),'color'=>'#FF0000'),
					'keyword4'=>array('value'=>urlencode($keyword4),'color'=>'#FF0000'),
					'remark'=>array('value'=>urlencode($remark)),
				)
			);
			$json_template=json_encode($template);
			$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token2;
			$res=$this->curl_post($url,urldecode($json_template));
		}
	}

	//微信发送任务提醒 designed by:zhangsen 2018-05-17
	function send_task_notice($openid,$url,$first,$keyword1,$keyword2,$keyword3,$keyword4,$remark){
		$ip=$this->get_ip();
		if(empty($ip) || $_SERVER['SERVER_ADDR']=='192.168.0.7')
		{
			//获取access_token
			if($_COOKIE['access_token']){
				$access_token2=$_COOKIE['access_token'];
			}else{
				$json_token=$this->curl_post("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa9104bb1d0267056&secret=1e77aa81ba98a0f0fd8699dc41d1ad87");
				$access_token1=json_decode($json_token,true);
				$access_token2=$access_token1['access_token'];
				setcookie('access_token',$access_token2,7200);
			}
			//模板消息
			$template=array(
				'touser'=>$openid,//用户openid
				'template_id'=>"rSoS-X_sTdHgHeYVgTvVk9idy94NIBgbnpX20fnQEFc",//在公众号下配置的模板id
				'url'=>$url,//点击模板消息会跳转的链接
				'topcolor'=>"#7B68EE",
				'data'=>array(
					'first'=>array('value'=>urlencode($first),'color'=>"#FF0000"),
					'keyword1'=>array('value'=>urlencode($keyword1),'color'=>'#FF0000'),//keyword需要与配置的模板消息对应
					'keyword2'=>array('value'=>urlencode($keyword2),'color'=>'#FF0000'),
					'keyword3'=>array('value'=>urlencode($keyword3),'color'=>'#FF0000'),
					'remark'=>array('value'=>urlencode($remark)),
				)
			);
			$json_template=json_encode($template);
			$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token2;
			$res=$this->curl_post($url,urldecode($json_template));
		}
	}

	//获取当前日期的上一周期或者下一周期(参数3:1上一周期/2下一周期,参数4:周期/整年/整月/整周) designed by:zhanglijing 2017-02-10
	function start_end_cycle_adjust($start_end,$time,$type,$way){
		if($way==1)
		{
			if($type==1)
			{
				if($start_end==1)
				{
					$date=$this->get_date("Y-m-26","-1 month",$time);
				}
				else
				{
					$date=$this->get_date("Y-m-25",NULL,$time);
				}
			}
			elseif($type==2)
			{
				if($start_end==1)
				{
					$date=$this->get_date("Y-m-26","+1 month",$time);
				}
				else
				{
					$date=$this->get_date("Y-m-25","+2 month",$time);
				}
			}
		}
		elseif($way==2)
		{
			if($type==1)
			{
				if($start_end==1)
				{
					$date=$this->get_date("Y-01-01","-1 year",$time);
				}
				else
				{
					$date=$this->get_date("Y-12-31","-1 year",$time);
				}
			}
			elseif($type==2)
			{
				if($start_end==1)
				{
					$date=$this->get_date("Y-01-01","+1 year",$time);
				}
				else
				{
					$date=$this->get_date("Y-12-31","+1 year",$time);
				}
			}
		}
		elseif($way==3)
		{
			if($type==1)
			{
				if($start_end==1)
				{
					$date=$this->get_date("Y-m","-1 month",$time);
				}
				else
				{
					$date=$this->get_date("Y-m","-1 month",$time);
				}
			}
			elseif($type==2)
			{
				if($start_end==1)
				{
					$date=$this->get_date("Y-m","+1 month",$time);
				}
				else
				{
					$date=$this->get_date("Y-m","+1 month",$time);
				}
			}
		}
		elseif($way==4)
		{
			if($type==1)
			{
				$date=$this->get_date("Y-m-d","-7 day",$time);
			}
			elseif($type==2)
			{
				$date=$this->get_date("Y-m-d","+7 day",$time);
			}
		}
		return $date;
	}

	function audit_status_judge($arrive_status){
		if($arrive_status==-1){
			$up_where['post_uid']=NULL;
			$up_where['post_date']=NULL;
			$up_where['post_user']=NULL;
		}else{
			for($i=1;$i<=$arrive_status;$i++)
			{
				if($i==$arrive_status){
					switch($arrive_status){
						case $i:
							$up_where['audit'.$i.'_uid']=NULL;
							$up_where['audit'.$i.'_user']=NULL;
							$up_where['audit'.$i.'_date']=NULL;
							$up_where['audit'.$i.'_message']=NULL;
						break;
					}
				}
			}
		}
		return $up_where;
	}

	function audit_status_judge_all($arrive_status,$dh_status){
		if($arrive_status==-1){
			$up_where['post_uid']=NULL;
			$up_where['post_date']=NULL;
			$up_where['post_user']=NULL;
			for($i=1;$i<=$dh_status;$i++)
			{
				$up_where['audit'.$i.'_uid']=NULL;
				$up_where['audit'.$i.'_user']=NULL;
				$up_where['audit'.$i.'_date']=NULL;
				$up_where['audit'.$i.'_message']=NULL;
			}
		}else{
			for($i=$arrive_status;$i<=$dh_status;$i++)
			{
				$up_where['audit'.$i.'_uid']=NULL;
				$up_where['audit'.$i.'_user']=NULL;
				$up_where['audit'.$i.'_date']=NULL;
				$up_where['audit'.$i.'_message']=NULL;
			}
		}

		return $up_where;
	}
	//查出运营管理中心旗下的部门 add by:chenyan 2018-08-21
	function get_all_deptname(){
		$db_department=M('db_department');
		$where['PART_DEPTID_CHN']='130005';
		$where['EFF_STATUS']='A';
		$result=$db_department->field('DEPTID as id')->where($where)->order('SORT')->select();
		$did_list=array();
		foreach($result as $k=>$re){
			array_push($did_list,$re['id']);
		}
		$temp = implode(",",$did_list);
		$where_1['_string']="PART_DEPTID_CHN IN (".$temp.")";
		$deptlist=$db_department->field('DEPTID as id,DEPT_DESCR as first_name')->where($where_1)->order('SORT')->select();
		return $deptlist;
	}
	//同步数据到project_department_map表 add by:chenyan 2018-08-21
	function syn_info_project_department_map($pid,$did){
		$db=M('db_project_department_map');
		$where['ProjectId']=$pid;
		$where['DeptId']=$did;
		$db->add($where);
	}
	//获取项目所属部门名 add by:chenyan 2018-08-22
	function get_dept_name($did){
		$db_department=M('db_department');
		$where['DEPTID']=$did;
		$where['EFF_STATUS']='A';
		$result=$db_department->field('DEPT_DESCR as first_name')->where($where)->order('SORT')->find();
		return $result;
	}
	//同步数据到db_project_user表 通过项目与部门，获取项目与具体人 add by:chenyan 2018-08-22
    function syn_info_project_user($pid,$did){
        //根据did获取部门下的人员
        $db_user=M('db_user');
        $db_project_user=M('db_project_user');
        $where_u['department_id']=$did;
        $where_u['status']=1;//在职状态判断
        $where_u['_string']='disable_date IS NULL';
        $result=$db_user->field('id as uid,position_descr as position,rank_descr as rank')->where($where_u)->select();
        if(!empty($result)){//部门里面有人员才会做管理
            foreach($result as $k=>$re){
                //拼接插入数据
                $result[$k]['nature']='临时';
                $label2_list = $this->get_post_from_label2(106,$re['position']);
                $result[$k]['post']=$label2_list['id'];
                $result[$k]['pid']=$pid;
                $result[$k]['come_date']=date("Y-m-d",time());
                $result[$k]['add_date']=date("Y-m-d H:i:s",time());
                $result[$k]['add_uid']=$this->login_id;
                //修改现有派工状态
                $this->change_out_date_by_uid($re['uid']);
                //添加新的派工状态
                $db_project_user->add($result[$k]);
            }
        }
    }
    //根据first_id和second_name获取id add by:chenyan 2018-08-22
    function get_post_from_label2($fid,$second_name){
        $db=M('db_universal_label2');
        $where_l['first_id']=$fid;
        $where_l['second_name']=$second_name;
        $result=$db->field('id')->where($where_l)->find();
        return $result;
    }
    //同步数据时，把现有的派工状态改成截至到当日完结 add by:chenyan 2018-08-22
    function change_out_date_by_uid($uid){
        $db_project_user=M('db_project_user');
        $where['uid']=$uid;
        $where['_string']='out_date IS NULL';
        $result=$db_project_user->field('id')->where($where)->find();//查询数据
        $where_update['id']=$result['id'];
        $where_update['out_date']=date("Y-m-d",time());
        $db_project_user->save($where_update);
    }
    /**
     * 日志写入
     * @author liubao 2018-8-23
     * @param $message //日志内容 例：编辑用户信息
     * @param $data //日志数据  以json格式为佳
     */
    public function add_log($message, $data)
    {
        $where['action'] = __ACTION__;
        $where['message'] = $message;
        $where['data'] = json_encode($data);
        $where['userId'] = isset($this->login_id)?$this->login_id:'0';
        $where['createdTime'] = date('Y-m-d H:i:s');
        $where['ip'] = $_SERVER["REMOTE_ADDR"];
        M("db_log")->add($where);
    }

    /**
     * 根据部门ID查项目关联表的项目ID
     * @param $deptid //部门ID
     * @return string
     */
    public function get_pid_by_deptid($deptid)
    {
        $res = M("db_project_department_map")->field("ProjectId")->where("DeptId=$deptid")->find();
        return $res['ProjectId'];
    }

    //根据用户ID和out_date is null 获取pro_user表的项目pid
    public function get_pid_byUidAndOutDate($uid)
    {
        $result = M("db_project_user")->field("id,pid")->where("uid=$uid AND out_date IS NULL")->find();
        return $result;
    }

    //获取项目关联表的信息
    public function get_department_map($field, $where)
    {
        $result = M("db_project_department_map")->field($field)->where($where)->find();
        return $result[$field];
    }

    //更新pro_user表信息
    public function update_pro_user($where, $data)
    {
        $result = M("db_project_user")->where($where)->save($data);
        return $result;
    }

    public function add_pro_user($res_user)
    {
        $where['DeptId'] = $res_user['department_id'];
        $map_ProjectId = $this->get_department_map('ProjectId', $where);//拿用户的部门ID获取关联表的pid
        if ($map_ProjectId) {//如果有pid就要把这个人新增到pro_user表
            $pro_user_add['uid'] = $res_user['id'];
            $pro_user_add['pid'] = $map_ProjectId;
            $pro_user_add['position'] = $res_user['position_descr'];
            $post = $this->get_post_from_label2('106', $res_user['position_descr']);
            $pro_user_add['post'] = $post['id'];
            $pro_user_add['rank'] = $res_user['rank_descr'];
            $pro_user_add['nature'] = '临时';
            $pro_user_add['come_date'] = date('Y-m-d');
            $pro_user_add['add_uid'] = isset($this->login_id) ? $this->login_id : '0';
            $pro_user_add['add_date'] = date('Y-m-d H:i:s');
            M("db_project_user")->add($pro_user_add);
        }
    }

    public function do_pro_user($where, $updateOrAdd)
    {
        $db_user = M("db_user");
        $res_user = $db_user->where($where)->find();
        if (isset($res_user) && $res_user['center_id'] == '130005') {//检测是否是目的地运营管理中心的人
            if ($updateOrAdd == 'add') {
                $this->add_pro_user($res_user);
            }
            if ($updateOrAdd == 'update') {
                $res_pro_user = $this->get_pid_byUidAndOutDate($res_user['id']);//拿用户id获取项目pid和id
                if (isset($res_pro_user)) {
                    $where_map['ProjectId'] = $res_pro_user['pid'];//项目关联表条件
                    $deptId = $this->get_department_map('DeptId', $where_map);//根据项目pid获取关联表的部门DeptId
                    if ($deptId != $res_user['department_id']) {//如果关联表部门ID与用户部门ID不一致，pro_user表对应记录的out_date要更新为当天
                        $where_update_pro_user['id'] = $res_pro_user['id'];
                        $update_pro_user['out_date'] = date('Y-m-d');
                        try {
                            $this->update_pro_user($where_update_pro_user, $update_pro_user);
                            $this->add_pro_user($res_user);
                        } catch (\Exception $e) {
                            $this->add_log('更新和新加项目关联表记录失败', '');
                        }
                    }
                }
            }
        }
    }
    /**
     * 将二维数组数据导出到excel
     * @param string $fileName 文件名
     * @param array $headArr Excel表头
     * @param array $data 写入excel的数据
     * @return null
     *	add by chenyan	2018-08-27
     */
    function downloaddExcel($fileName,$headArr,$data){
        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");

        //对数据进行检验
        if(empty($data) || !is_array($data)){
            die("data must be a array");
        }
        //检查文件名
        if(empty($fileName)){
            exit;
        }

        $date = date("Y_m_d",time());
        $fileName .= "_{$date}.xls";

        //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);//左对齐
        $objPHPExcel->getActiveSheet()->getColumnDimension(\PHPExcel_Cell::stringFromColumnIndex(0))->setAutoSize(true);//自动宽度
        $objProps = $objPHPExcel->getProperties();
        //设置表头
        $key = ord("A");
        foreach($headArr as $v){
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum.'1', $v);
			$objPHPExcel->getActiveSheet()->getColumnDimension($colum)->setWidth(12);
            $key += 1;
        }
        $column = 2;
        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach($data as $key => $rows){ //行写入
            $span = ord("A");
            foreach($rows as $keyName=>$value){// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j.$column, $value);
                $span++;
            }
            $column++;
        }
		/*月收入表总计*/
		// $sum=getIncomeAll();
		// $objPHPExcel->getActiveSheet()->setCellValue("A".($column), '总计:'.$sum);
        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        exit;
    }
	/*
		将config中的常量转换成循环可读形式
		$list 常量结果集合
		add by chenyan 2018-09-06
	*/
	public function get_constant($list){
		$result = array();
		foreach($list as $k=>$l){
			$result[$k]['id']=$k;
			$result[$k]['name']=$l;
		}
		return $result;
	}
	/*
		将提交的空值过滤，需要提交的值和数据库值一致
		参数: $result 需要洗的结果集合
			  $arr 返回值存放的数组
		返回：有值的数组
		add by chenyan 2018-09-06
	*/
	public function unset_null($result,$arr){
		foreach($result as $k=>$r){  
			if(!$r){
				unset($result[$k]);  
			}else{
				$arr[$k]=$r;
			}  
		}
		return $arr;
	}
	/*
		分页通用方法
		参数 ：$result 结果集合
		返回值：result：限定过展示数的结果集
		        show：分页元素
		add by chenyan 2018-09-07
	*/
	public function show_page($result){
		$page_count = C("PAGE_LIST_ROWS");
		$count=count($result);
		$Page = new \Think\Page($count,$page_count);
		$re['result']=array_slice($result,$Page->firstRow,$Page->listRows);
		$re['show']=$Page->show();
		return $re;
	}
	/*
		获取市场信息编号
		返回值:	$str:市场编号最新值
		add by chenyan 2018-09-10
	*/
	public function get_new_number(){
		$db_result=M('db_crm_customers_information');

		$result=$db_result->field('max(customer_num) as customer_num')->find();

		if(empty($result))
		{
			$num='00001';
		}
		else
		{
			$num=substr($result['customer_num'],3,5)+1;
			if(strlen($num)==1){$num='0000'.$num;}
			elseif(strlen($num)==2){$num='000'.$num;}
			elseif(strlen($num)==3){$num='00'.$num;}
			elseif(strlen($num)==4){$num='0'.$num;}
		}
		$str='MSX'.$num;
		return $str;
	}
	/*
		获取市场信息对应的项目名
		参数：$project_id 项目id
		返回：结果集project_num 项目编号+ project_id项目id
		add by chenyan 2018-09-10
	*/
	public function getProjectNameByInfoId($project_id){
		$db_crm_project=M('db_crm_project');
		$where_p['project_id']=$project_id;
		$re = $db_crm_project->field("project_num,project_id")->where($where_p)->find();
		return $re;
	}
	/*
		获取项目对应的联系人
		参数：linkman 联系人编号
		返回 结果集联系人的信息
		add by chenyan 2018-09-11
	*/
	public function getContactByProjectLinkman($linkman){
		$db_crm_contact = M('db_crm_contact');
		$where["_string"]="contact_id in (".$linkman.")";
		$re = $db_crm_contact->field("contact_id,username,mobilephone")->where($where)->select();
		return $re;
	}
	/*
		获取项目的id，项目名字
		参数:	pid	具体项目id
		返回 项目对应的id和项目名（全体，传id获取单条），项目进度，项目参与人
		add by chenyan 2018-09-11
	*/
	public function getProjectInfo($pid=""){
		$db_crm_project=M('db_crm_project');
		if($pid!=""){
			$where['project_id']=$pid;
		}else{
			$where="";
		}
		$re = $db_crm_project->field("name,project_id,stage,other_linkman,main_linkman")->where($where)->select();
		return $re;
	}
	/*
		更新项目流程
		参数：pid 		项目id
			  status	项目状态
			  remark	备注
			  au		添加人id
			  aname		添加人姓名
			  adate		添加时间
		add by chenyan	2018-09-12
	*/
	public function update_project_flow($pid,$status,$remark,$au,$aname,$adate){
		$db_crm_project_flow=M('db_crm_project_flow');
		$where['project_id']=$pid;
		$where['status']=$status;
		$where['remark']=$remark;
		$where['add_uid']=$au;
		$where['add_user']=$aname;
		$where['add_date']=$adate;
		$db_crm_project_flow->add($where);
	}
	/*
		统计时间，计算当前时间在1年中的第几周，以及本周的开始日期和结束日期
		参数 $now 格式：date("Y-m-d")
		返回值：当前年份，当日在整年中的第几周，本周的开始日期和结束日期
		add by chenyan	2018-09-13
	*/
	public function getNowTimeInfo($now){
		$str = array();
		//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
		$str['year']=date('Y',strtotime($now));
		$first=1;
		//当日在整年中的第几周
		$str['week']=date('W',strtotime($now));
		//获取当前周的第几天 周日是 0 周一到周六是 1 - 6
		$w=date('w',strtotime($now));
		//获取本周开始日期，如果$w是0，则表示周日，减去 6 天
		$week_start=date('Y-m-d',strtotime("$now -".($w ? $w - $first : 6).' days'));
		$str['week_start']=$week_start;
		//本周结束日期
		$week_end=date('Y-m-d',strtotime("$week_start +6 days"));
		$str['week_end']=$week_end;
		return $str;
	}
	/*根据项目id获取其对应的联系人信息*/
	public function getContactInfoByProjectId($pid){
		$result=$result1=$result2= "";
		$project = $this->getProjectInfo($pid);
		if($project[0]['main_linkman']!=""){
			$result1 = $project[0]['main_linkman'];
		}
		if($project[0]['other_linkman']!=""){
			$result2 = $project[0]['other_linkman'];
		}
		if($result2!=""){
			$result= $result1.",".$result2;
		}else{
			$result=$result1;
		}
		return $result;
	}
	/*
		根据联系人id获取其对应的项目名
	*/
	public function getProjectNameByContactId($cid){
		$str = "";
		$db_crm_project=M('db_crm_project');
		$where['other_linkman']=array('like','%'.$cid.'%');
		$result = $db_crm_project->field("name")->where($where)->select();
		foreach($result as $r){
			$str = $str.$r['name']."<br>";
		}
		return $str;
	}
	public function getProjectInfoByContactId($cid){
		$db_crm_project=M('db_crm_project');
		$where['other_linkman']=array('like','%'.$cid.'%');
		$result = $db_crm_project->field("name,project_id")->where($where)->select();
		return $result;
	}
	/*
		更新project表中的联系人
		add by chenyan	2018-09-18
	*/
	public function update_project_contact($pid,$cid){
		$db_crm_project=M('db_crm_project');
        $result_old = $db_crm_project->where("project_id=$pid")->find();
		$data['project_id']=$pid;
		if($result_old['other_linkman']==""){
			$data['other_linkman'] = $cid;
		}else{
			$data['other_linkman'] = $result_old['other_linkman'].",".$cid;
		}
		$db_crm_project->save($data);
	}
	public function change_contact($pid,$cid){
		$db_crm_project=M('db_crm_project');
		$result_old = $db_crm_project->where("project_id=$pid")->find();
		$list = explode(",",$result_old['other_linkman']);
		foreach($list as $k =>$l){
			if($l==$cid){
				unset($list[$k]);
			}
		}
		$data['other_linkman'] = implode(",",$list);
		$data['project_id']=$pid;
		$db_crm_project->save($data);
	}
	/*
		检查联系人的手机或者座机是否有重复
		add by chenyan	2018-09-18
		参数 	$phone  用户手机或座机信息
				$cid	当前编辑人的id（新增时为空）
				$type	1检查mobile，2检查phone
	*/
	public function checkPhoneNumber($phone,$type,$cid=""){
		$db_contact = M("db_crm_contact");
		if($type==1)
		{
			$where_a['mobilephone']=$phone;
			$search_a=$db_contact->where($where_a)->find();
			if(!empty($search_a))
			{
				if($search_a["contact_id"]!=$cid){
					$data['status']=self::NO;
					$data['msg']='同一中心手机号重复，该人员为'.$search_a['username'];
					echo json_encode($data);die;
				}
			}
		}
		elseif($type==2)
		{
			$where_a['telephone']=$phone;
			$search_b=$db_contact->where($where_a)->find();
			if(!empty($search_b))
			{
				if($search_a["contact_id"]!=$cid){
					$data['status']=self::NO;
					$data['msg']='同一中心座机号重复，该人员为'.$search_b['username'];
					echo json_encode($data);die;
				}
			}
		}
		//检查成功的
		$data['status']=self::YES;
		$data['msg']='操作成功';
		echo json_encode($data);die;
	}
	public function get_user_name($cid){
		$db_crm_contact=M('db_crm_contact');
        $result = $db_crm_contact->where("contact_id=$cid")->find();
		return $result;
	}
	/*获取项目名income用，后期做兼容*/
	public function getAllProjectNameByIncome($re){
		$db_old_project=M('db_project');
		$db_new_project=M('db_crm_contact');
		$field="project_num,project_name";
		foreach($re as $key=>$r){
			$where = "";
			$where['id']=$r['pid'];
			$result = $db_old_project->field($field)->where($where)->find();
			$re[$key]['name'] = $result['project_num']."-".$result['project_name'];
		}
		return $re;
	}
	public function getProjectNameByIncome($pid){
		$db_old_project=M('db_project');
		$db_new_project=M('db_crm_contact');
		$field="project_num,project_name";
		$where['id']=$pid;
		$result = $db_old_project->field($field)->where($where)->find();
		$re =  $result['project_num']."-".$result['project_name'];
		return $re;
	}
	
}
?>